// 切换到 iot 数据库
db = db.getSiblingDB('iot');

// 创建用户
db.createUser({
  user: 'iot_user',
  pwd: 'iot_password',
  roles: [
    {
      role: 'readWrite',
      db: 'iot'
    }
  ]
});

// 创建集合
db.createCollection('devices');
db.createCollection('deviceData');
db.createCollection('users');

// 创建索引
db.devices.createIndex({ deviceId: 1 }, { unique: true });
db.devices.createIndex({ userId: 1 });
db.deviceData.createIndex({ deviceId: 1, timestamp: -1 });
db.deviceData.createIndex({ userId: 1 });
db.users.createIndex({ email: 1 }, { unique: true });
db.users.createIndex({ username: 1 }, { unique: true });

print('IoT database initialized with devices, deviceData, and users collections');
